﻿using MiniFox.Caching;
using MiniFox.Configurations;
using MiniFox.Infrastructure;
using MiniFox.Net;
using MiniFox.Net.Http;
using MiniFox.Platform.Configurations;
using MiniFox.Resources;
using MiniFox.Web;
using System.Net.Http.Headers;
using System.Net.Http.Json;

namespace MiniFox.Platform
{
    /// <summary>
    /// 
    /// </summary>
    public class WebApiClient : HttpService
    {
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="setting"></param>
        /// <param name="adapter"></param>
        public WebApiClient(IServiceSetting setting, HttpAdapter adapter): base(setting, adapter)
        {

        }
        /// <summary>
        /// 提取access token
        /// </summary>
        /// <returns></returns>
        protected virtual string GetAccessToken()
        {
            var accessTokenUrl = this.WebApiClientSetting.FullAccessTokenUrl;
            var uri = new Uri(accessTokenUrl);
            var query = new Dictionary<string, object>
            {
                { OpenApiKey.OPEN_APP_ID, this.WebApiClientSetting.ApplicationID },
                { OpenApiKey.OPEN_SECRET_KEY, this.WebApiClientSetting.SecretKey }
            };
            var httpContent = uri.HttpGet(query);
            var response = httpContent.ReadFromJsonAsync<Response<string>>().Result;
            if (!response.Success)
            {
                throw new MiniFox.Exceptions.SystemException(string.IsNullOrEmpty(response.Message) ? CommonMessages.MSG_SysError : response.Message);
            }
            return response.Data;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected virtual string AccessToken
        {
            get
            {
                Func<string> func = this.GetAccessToken;
                var key = $"{this.WebApiClientSetting.OrganizationID}#{this.WebApiClientSetting.ApplicationID}";
                var accessToken = this.WebApiClientSetting.EnableCache ? CacheService.GetCacheObject<string>(key, func, this.WebApiClientSetting.RenewInterval) : func();
                return accessToken;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="headers"></param>
        protected override void SetDefaultHeaders(HttpRequestHeaders headers)
        {
            base.SetDefaultHeaders(headers);
            headers.Authorization = new AuthenticationHeaderValue(OpenAuthKey.AUTH_SCHEME, this.AccessToken);
        }
        /// <summary>
        /// 
        /// </summary>
        protected internal virtual WebApiClientSetting WebApiClientSetting
        {
            get
            {
                return Configuration[this.WebAdapter.Name];
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static WebApiClientConfiguration Configuration
        {
            get
            {
                return ConfigurationFactory.GetConfig<WebApiClientConfiguration>();
            }
        }
    }
}
